<?php

/**
 * This is the model class for table "site".
 *
 * The followings are the available columns in table 'site':
 * @property integer $sit_code
 * @property string $sit_name
 * @property string $sit_slogan
 * @property string $sit_wellcomeTitle
 * @property string $sit_wellcomeText
 * @property string $sit_title
 * @property string $sit_footerText
 *
 * The followings are the available model relations:
 * @property Page[] $pages
 */
class Site extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Site the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'site';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('sit_name,sit_title,tem_code,sit_logo', 'required'),
			array('sit_code,tem_code', 'numerical', 'integerOnly'=>true),
			array('sit_name, sit_slogan, sit_wellcomeTitle', 'length', 'max'=>245),
			array('sit_title', 'length', 'max'=>145),
			array('sit_logo', 'file', 'types'=>'jpg, gif, png'),
			array('sit_wellcomeText, sit_footerText', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('sit_code, sit_name, sit_slogan, sit_wellcomeTitle, sit_wellcomeText, sit_title, sit_footerText', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'pages' => array(self::HAS_MANY, 'Page', 'sit_code'),
			'template' => array(self::BELONGS_TO, 'Template', 'tem_code'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'sit_code' => Yii::t('site','Code'),
			'sit_name' => Yii::t('site','Name'),
			'sit_slogan' => Yii::t('site','Slogan'),
			'sit_wellcomeTitle' => Yii::t('site','WellcomeTitle'),
			'sit_wellcomeText' => Yii::t('site','Content'),
			'sit_title' => Yii::t('site','Title'),
			'sit_footerText' => Yii::t('site','Footer'),
			'tem_code' => Yii::t('site','Template'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('sit_code',$this->sit_code);
		$criteria->compare('sit_name',$this->sit_name,true);
		$criteria->compare('sit_slogan',$this->sit_slogan,true);
		$criteria->compare('sit_wellcomeTitle',$this->sit_wellcomeTitle,true);
		$criteria->compare('sit_wellcomeText',$this->sit_wellcomeText,true);
		$criteria->compare('sit_title',$this->sit_title,true);
		$criteria->compare('sit_footerText',$this->sit_footerText,true);
    $criteria->condition = 'usr_code='.Yii::app()->user->id;

    

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	
	public function isOwner($siteCode){
    $criteria=new CDbCriteria;
    $criteria->condition = 'usr_code='.Yii::app()->user->id.' AND sit_code='.(int)$siteCode;
    return (boolean)Site::model()->count($criteria);
  
  }
}